import { DBPost } from '../../../db/DBPost.js';
var app=getApp()
Page({
    data: {
        isPlayingMusic: false
    },
    onLoad: function(options) {
        var postId = options.id,
            list;
        this.postId = postId;
        this.dbPost = new DBPost(postId);
        this.list = this.dbPost.getPostItemById().data;
        this.setData({
            artcle: this.list
        })
        this.setAniation();
        this.initMusic();
        this.MusicMonitor();
    },
    onReady: function() {
        wx.setNavigationBarTitle({
            title: this.list.title,
        })
    },
    initMusic:function(){
      var id = this.list.postId;
      if (app.globalData.isplay && app.globalData.artcleId===id){
        this.setData({
          isPlayingMusic:true
        })
      }else{
        this.setData({
          isPlayingMusic: false
        })
      }
    },
    setAniation: function() {
        var animationUp = wx.createAnimation({
            timingFunction: 'ease-in-out'
        })
        this.animationUp = animationUp
    },
    collecttap: function() {
        var newData = this.dbPost.collect();
        this.setData({
            'artcle.collectionStatus': newData.collectionStatus,
            'artcle.collectionNum': newData.collectionNum
        })
        wx.showToast({
            title: newData.collectionStatus ? "收藏成功" : "取消收藏",
            duration: 1000,
            icon: 'success',
            mask: true
        })
    },
    uptap: function() {
        var newData = this.dbPost.up();
        this.setData({
            "artcle.upStatus": newData.upStatus,
            "artcle.upNum": newData.upNum
        })
        this.animationUp.scale(2).step(); //动画:放大2倍,.step()结束
        this.setData({
            animationUp: this.animationUp.export() //输出
        })
        setTimeout(function(){
           this.animationUp.scale(1).step(); 
        this.setData({
            animationUp: this.animationUp.export() 
        })
        }.bind(this),300)
    },
    tocomment:function(event){
      var id=this.postId;
      console.log(id)
     wx.navigateTo({
      url:'../comment/comment?id='+id,
     })
    },
    musictap:function(){
        if(this.data.isPlayingMusic){
            wx.pauseBackgroundAudio();
            this.setData({
              isPlayingMusic: false
            })
            app.globalData.isplay=false
        }else{
          wx.playBackgroundAudio({
            dataUrl: this.list.music.url,
            title: this.list.music.title,
            coverImgUrl: this.list.music.coverImg
          })
          this.setData({
            isPlayingMusic: true
          })
          app.globalData.isplay = true;
          app.globalData.artcleId=this.list.postId
        }
    },
//监听,中止(all) 暂停和播放(this) 注意回调函数中的this
    MusicMonitor:function(){
      var self=this;
      wx.onBackgroundAudioStop(function(){
        that.setData({
          isPlayingMusic:false
        })
        app.globalData.isplay = false;
      });
      wx.onBackgroundAudioPlay(function(){
        if (app.globalData.artcleId === self.list.postId){
          that.setData({
            isPlayingMusic: true
          })
          app.globalData.isplay = true;
        }
      })
      wx.onBackgroundAudioPause(function(){
        if (app.globalData.artcleId === self.list.postId) {
          that.setData({
            isPlayingMusic: false
          })
          app.globalData.isplay = false;
        }
      })
    },
  onShareAppMessage:function(){
    var id =this.list.postId;
    return {
      title: '自定义转发标题',
      path: '/pages/text/text-detail/text-detail' + id, 
    }  
  }
})